package com.bqy.myokgo.base.manager;

import android.app.Activity;
import android.app.Dialog;

import com.bqy.myokgo.utils.ListUtils;
import com.bqy.myokgo.utils.MyTool;
import com.lzy.okgo.OkGo;

import java.util.ArrayList;

/**
 * 熊猫先生
 * 2018/10/23:14:33
 */

public class DialogManager {
    public static ArrayList<String> listHttp = null;
    public static DialogManager instance;
    public static Dialog dialog;

    /**
     * 单一实例
     */
    public static DialogManager getInstance() {
        if (instance == null) {
            instance = new DialogManager();
        }
        return instance;
    }

    /**
     * 添加
     */
    public void addHttpIS(String s, boolean isDialog, Activity activity) {
        if (listHttp == null) {
            listHttp = new ArrayList<String>();
        }
        if (isDialog) {
            if (dialog == null) {
                dialog = MyTool.initDialog(activity);
                if (!dialog.isShowing()) {
                    dialog.show();
                }
            } else {
                if (!dialog.isShowing()) {
                    dialog.show();
                }
            }
        }
        listHttp.add(s);
    }

    /**
     * 添加
     */
    public void addHttp(String s, Activity activity) {
        if (listHttp == null) {
            listHttp = new ArrayList<String>();
        }
        if (dialog == null) {
            dialog = MyTool.initDialog(activity);
            if (!dialog.isShowing()) {
                dialog.show();
            }
        } else {
            if (!dialog.isShowing()) {
                dialog.show();
            }
        }
        listHttp.add(s);
    }

    /**
     * 删除
     */
    public void RemoveHttp(String TAG) {
        if (ListUtils.isEmpty(listHttp)) {
            listHttp.clear();
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
                dialog = null;
            }
        } else {
            if (listHttp.size() == 1) {
                listHttp.clear();
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                    dialog = null;
                }
            } else {
                for (int i = 0; i < listHttp.size(); i++) {
                    if (TAG.equals(listHttp.get(i))) {
                        listHttp.remove(i);
                    }
                }
            }
        }
    }

    /**
     * 清除所有
     */
    public void RemoveHttpAll() {
        if (!ListUtils.isEmpty(listHttp)) {
            listHttp.clear();
        }
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    /**
     * 清除所有请求
     */
    public void RemoveOkGoAll() {
        if (ListUtils.isEmpty(DialogManager.listHttp)) {
            return;
        }
        for (int i = 0; i < DialogManager.listHttp.size(); i++) {
            OkGo.getInstance().cancelTag(DialogManager.listHttp.get(i));
        }
    }
}
